package com.sevenGroup.pawnshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevenGroup.pawnshop.Utils.Result;
import com.sevenGroup.pawnshop.controller.form.UserForm;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.Account;
import com.sevenGroup.pawnshop.model.Users;
import com.sevenGroup.pawnshop.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/users")
@Api(tags = "用户相关")
public class UsersController {
    @Resource
    private UsersService usersService;

//    未登陆回给前端405状态码
    @GetMapping("nologin")
    @ApiOperation("发送没有登陆的请求")
    public Result noLogin(){
        return Result.fail(null).setState(405);
    }

//    通过账号ID查询用户对象
    @PostMapping("getUserByaid")
    @ApiOperation("查询用户对象")
    public Result getUserByaid(@ApiParam("传入账号ID") Integer aid){
//        通过账号ID查询用户对象
        Users user=usersService.getUserByaid(aid);
        return Result.success(user);
    }

//    查询所有账号
    @PostMapping("selectAllUser")
    @ApiOperation("查询所有用户")
    public Result selectAllUser(Integer userId){
        List<Users> user_id = usersService.list(new QueryWrapper<Users>().notIn("user_id", userId));
        return Result.success(user_id);
    }

    @PostMapping("changeRole")
    @ApiOperation("给用户分配角色")
    @PreAuthorize("hasAnyAuthority('boss')")
    public Result changeRole(@RequestBody UserForm userForm) throws sevenGroupException {
        usersService.changeRole(userForm);
        return Result.success(null);
    }

}

